package com.zyx.inoutstudy;

import java.io.*;

public class TestUtils {
    /*
     * 复制一个文件，从a路径到b路径
     */
    public static void copytext(String a, String b) {
        FileInputStream fis = null;
        FileOutputStream fos = null;

        //拼接文件名
        int lastIndex = a.lastIndexOf("\\");
        String filename = a.substring(lastIndex + 1);
        b = b + "\\" + filename;

        try {
            File file = new File(b);
            if (!file.exists()) {
                file.createNewFile();
            }
            fis = new FileInputStream(a);
            fos = new FileOutputStream(file);
            byte[] bs = new byte[1024];
            int length = 0;
            while ((length = fis.read(bs)) != -1) {
                fos.write(bs, 0, length);
            }

        } catch (FileNotFoundException e) {
            System.out.println("文件路径不存在，请核实");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("连接超时，请稍后再试....");
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                System.out.println("连接超时，请稍后再试...");
            }
        }

    }
}